package handler

import (
	"net/http"

	"chainmaker.org/chainmaker/smarteditor/audit"
	"chainmaker.org/chainmaker/smarteditor/editor"
	"chainmaker.org/chainmaker/smarteditor/file"
	"chainmaker.org/chainmaker/smarteditor/notification"
	"chainmaker.org/chainmaker/smarteditor/oauth"
	"chainmaker.org/chainmaker/smarteditor/output"
	"chainmaker.org/chainmaker/smarteditor/session"
)

// RegisterHttpHandler
// register http handler to http server
func RegisterHttpHandler() {
	dispatchCenter := &oauth.AuthDispatchCenter{
		AuthEndpointMap: map[string]oauth.AuthServerHandler{},
	}
	gitLabEndpoint := new(oauth.GitLabAuthServerHandler)
	chainMakerEndpoint := new(oauth.ChainMakerAuthServerHandler)
	ssoEndPoint := new(oauth.SSOAuthServerHandler)
	dispatchCenter.AuthEndpointMap[gitLabEndpoint.GetAuthServerFlag()] = gitLabEndpoint
	dispatchCenter.AuthEndpointMap[chainMakerEndpoint.GetAuthServerFlag()] = chainMakerEndpoint
	dispatchCenter.AuthEndpointMap[ssoEndPoint.GetAuthServerFlag()] = ssoEndPoint
	// contract auditor
	audit.InitAuditAuditProcessor()

	// IDE
	//http.HandleFunc("/", CompressAndLogTimeCostInterceptor(indexHandler))
	http.HandleFunc("/getUserConfig", corsWrapper(CompressAndLogTimeCostInterceptor(indexHandler)))
	// static resources
	// http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
	// http.Handle("/static/users/", http.StripPrefix("/static/", http.FileServer(http.Dir(conf.Wide.Data+"/static"))))
	// serveSingleFile("/favicon.ico", "./static/images/favicon.png")
	// oauth
	// http.HandleFunc("/login", corsWrapper(TimeCostInterceptor(session.LoginHandler2)))
	// http.HandleFunc("/getConfig", corsWrapper(TimeCostInterceptor(session.LoginHandler)))
	http.HandleFunc("/getLoginUrl", corsWrapper(CompressAndLogTimeCostInterceptor(getLoginURLS)))
	http.HandleFunc("/login/redirect", corsWrapper(TimeCostInterceptor(dispatchCenter.LoginRedirectHandler)))
	http.HandleFunc("/login/callback", corsWrapper(TimeCostInterceptor(dispatchCenter.LoginCallbackHandler)))
	http.HandleFunc("/login/thirdPart", corsWrapper(TimeCostInterceptor(dispatchCenter.ThirdPartLoginHandler)))
	http.HandleFunc("/logout", corsWrapper(TimeCostInterceptor(session.LogoutHandler)))
	// session
	http.HandleFunc("/session/save", corsWrapper(TimeCostInterceptor(session.SaveContentHandler)))
	http.HandleFunc("/session/ws", corsWrapper(TimeCostInterceptor(session.WSHandler)))
	http.HandleFunc("/output/ws", corsWrapper(TimeCostAndAuthInterceptor(output.WSHandler)))
	http.HandleFunc("/notification/ws", corsWrapper(TimeCostAndAuthInterceptor(notification.WSHandler)))
	// use resource limit in code, because this method is asynchronous
	http.HandleFunc("/build", corsWrapper(TimeCostAndAuthInterceptor(output.BuildHandler)))
	http.HandleFunc("/buildAll", corsWrapper(TimeCostAndAuthInterceptor(session.BuildAllContractHandler)))
	http.HandleFunc("/invokeAll", corsWrapper(TimeCostAndAuthAndResourcesLimitInterceptor(output.InvokeAllContractHandler)))
	http.HandleFunc("/cross", corsWrapper(TimeCostAndAuthAndResourcesLimitInterceptor(output.CrossCompilationHandler)))
	http.HandleFunc("/crossanddeploy", corsWrapper(TimeCostAndAuthAndResourcesLimitInterceptor(output.CrossDeployHandler)))
	http.HandleFunc("/dockergo/method", corsWrapper(TimeCostAndAuthInterceptor(output.GetInvokeMethodHandler)))
	// deploy
	http.HandleFunc("/deploy/contractList", corsWrapper(TimeCostAndAuthInterceptor(output.DeployContractListHandler)))
	http.HandleFunc("/deploy", corsWrapper(TimeCostAndAuthInterceptor(output.CrossDeployHandler)))
	http.HandleFunc("/deploy/hasDeployContractList", corsWrapper(TimeCostAndAuthInterceptor(output.HasDeployContractListHandler)))
	http.HandleFunc("/deploy/deleteDeploy", corsWrapper(TimeCostAndAuthInterceptor(output.DeleteDeployHandler)))
	http.HandleFunc("/deploy/deleteBuild", corsWrapper(TimeCostAndAuthInterceptor(output.DeleteBuildHandler)))
	// record
	http.HandleFunc("/record/getContractByHash", corsWrapper(TimeCostAndAuthInterceptor(output.GetContractCodeByHashHandler)))
	// file tree
	http.HandleFunc("/files", corsWrapper(TimeCostAndAuthInterceptor(file.GetFilesHandler)))
	http.HandleFunc("/goModuleFiles", corsWrapper(TimeCostAndAuthInterceptor(file.GetGoModulesHandler)))
	http.HandleFunc("/compileDirectory", corsWrapper(TimeCostAndAuthInterceptor(file.GetCompileDirectory)))
	http.HandleFunc("/contractHasBuild", corsWrapper(TimeCostAndAuthInterceptor(session.ContractHasBuildHandler)))
	http.HandleFunc("/file/refresh", corsWrapper(TimeCostAndAuthInterceptor(file.RefreshDirectoryHandler)))
	http.HandleFunc("/file", corsWrapper(TimeCostAndAuthInterceptor(file.GetFileHandler)))
	http.HandleFunc("/file/save", corsWrapper(TimeCostAndAuthInterceptor(file.SaveFileHandler)))
	http.HandleFunc("/file/new", corsWrapper(TimeCostAndAuthInterceptor(file.NewFileHandler)))
	http.HandleFunc("/file/remove", corsWrapper(TimeCostAndAuthInterceptor(file.RemoveFileHandler)))
	http.HandleFunc("/file/rename", corsWrapper(TimeCostAndAuthInterceptor(file.RenameFileHandler)))
	// file export
	http.HandleFunc("/file/7z/new", corsWrapper(TimeCostAndAuthInterceptor(file.Create7zHandler)))
	http.HandleFunc("/file/7z", corsWrapper(TimeCostAndAuthInterceptor(file.Get7zHandler)))
	http.HandleFunc("/file/getContractFile", corsWrapper(TimeCostAndAuthInterceptor(file.GetContractFile)))
	//file import
	http.HandleFunc("/file/import", corsWrapper(TimeCostAndAuthInterceptor(file.ImportFileHandle)))
	//code
	http.HandleFunc("/go/fmt", corsWrapper(TimeCostAndAuthInterceptor(editor.GoFmtHandler)))
	http.HandleFunc("/autocomplete", corsWrapper(TimeCostAndAuthInterceptor(editor.AutocompleteHandler)))
	http.HandleFunc("/outline", corsWrapper(TimeCostAndAuthInterceptor(file.GetOutlineHandler)))

	//un use
	//http.HandleFunc("/about", corsWrapper(TimeCostAndAuthInterceptor(aboutHandler)))
	http.HandleFunc("/preference", corsWrapper(TimeCostAndAuthInterceptor(session.PreferenceHandler)))
	http.HandleFunc("/file/search/text", corsWrapper(TimeCostAndAuthInterceptor(file.SearchTextHandler)))
	http.HandleFunc("/file/find/name", corsWrapper(TimeCostAndAuthInterceptor(file.FindHandler)))
	http.HandleFunc("/stop", corsWrapper(TimeCostAndAuthInterceptor(output.StopHandler)))
	http.HandleFunc("/go/test", corsWrapper(TimeCostAndAuthInterceptor(output.GoTestHandler)))
	http.HandleFunc("/go/vet", corsWrapper(TimeCostAndAuthInterceptor(output.GoVetHandler)))
	http.HandleFunc("/go/install", corsWrapper(TimeCostAndAuthInterceptor(output.GoInstallHandler)))
	http.HandleFunc("/exprinfo", corsWrapper(TimeCostAndAuthInterceptor(editor.GetExprInfoHandler)))
	http.HandleFunc("/find/decl", corsWrapper(TimeCostAndAuthInterceptor(editor.FindDeclarationHandler)))
	http.HandleFunc("/find/usages", corsWrapper(TimeCostAndAuthInterceptor(editor.FindUsagesHandler)))
	http.HandleFunc("/file/zip/new", corsWrapper(TimeCostAndAuthInterceptor(file.CreateZipHandler)))
	http.HandleFunc("/file/zip", corsWrapper(TimeCostAndAuthInterceptor(file.GetZipHandler)))
	//for performance test
	http.HandleFunc("/createuser", TimeCostInterceptor(session.CreateUser))
	//
	http.HandleFunc("/contracts/getTemplateContracts", corsWrapper(TimeCostAndAuthInterceptor(file.GetContractTemplate)))
	// 合约代码审核
	http.HandleFunc("/auditContract", corsWrapper(TimeCostAndAuthInterceptor(audit.AuditHandler)))

	http.HandleFunc("/contracts/getHiddenList", corsWrapper(TimeCostAndAuthInterceptor(file.HiddenContractListHandler)))
	http.HandleFunc("/contracts/setHiddenStatus", corsWrapper(TimeCostAndAuthInterceptor(file.SetContractHiddenStatus)))

}
